package cn.xopencode.appletweb.controller.record.robotroom;

import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.rpc.robotroom.RobotRoomRecordRpc;
import cn.xopencode.recordservice.rpc.robotroom.dto.AdminRecordPageDTO;
import cn.xopencode.recordservice.rpc.robotroom.dto.RobotRoomRecordCreateDTO;
import cn.xopencode.recordservice.rpc.robotroom.dto.RobotRoomRecordPageDTO;
import cn.xopencode.recordservice.rpc.robotroom.dto.RobotRoomRecordUpdateDTO;
import cn.xopencode.recordservice.rpc.robotroom.vo.*;
import cn.xopencode.userservice.rpc.dept.DeptRpc;
import cn.xopencode.userservice.rpc.user.UserRpc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "智能机器人班机柜间巡检记录 API")
@RestController
@RequestMapping("/robotroom")
@Validated
public class RobotRoomRecordController {

    @DubboReference(version = "${dubbo.consumer.RobotRoomRecordRpc.version}")
    private RobotRoomRecordRpc robotRoomRecordRpc;
    @DubboReference(version = "${dubbo.consumer.UserRpc.version}")
    private UserRpc userRpc;
    @DubboReference(version = "${dubbo.consumer.DeptRpc.version}")
    private DeptRpc deptRpc;

    /**
     * 根据查询条件 分页获取所有的智能机器人班机柜间巡检记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllByQuery")
    @ApiOperation("根据查询条件分页获取所有的智能机器人班机柜间巡检记录")
    public CommonResult<PageResult<RobotRoomRecordPageVO>> getPageRobotRoomRecord(RobotRoomRecordPageDTO pageDTO){
        CommonResult<PageResult<RobotRoomRecordPageVO>> result = robotRoomRecordRpc.pageRobotRoomRecord(pageDTO);
        List<RobotRoomRecordPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getEmpIds())){
                pageVO.setEmpRels(userRpc.getEmployeeName(pageVO.getEmpIds()));
            }
        });
        return result;
    }

    /**
     *  新增 智能机器人班机柜间巡检记录
     * @param createDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增 智能机器人班机柜间巡检记录")
    public CommonResult<Integer> createRobotRoomRecord(@Valid RobotRoomRecordCreateDTO createDTO){
        return robotRoomRecordRpc.createRobotRoomRecord(createDTO);
    }

    /**
     *  更新 智能机器人班机柜间巡检记录
     * @param updateDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新 智能机器人班机柜间巡检记录")
    public CommonResult<Integer> updateRobotRoomRecord(@Valid RobotRoomRecordUpdateDTO updateDTO){
        return robotRoomRecordRpc.updateRobotRoomRecord(updateDTO);
    }

    /**
     * 根据id获取智能机器人班机柜间巡检记录详细
     * @param roomId
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation("根据id获取智能机器人班机柜间巡检记录详细")
    public CommonResult<RobotRoomRecordUpdateVO> getRobotRoomRecordById(String roomId){
        CommonResult<RobotRoomRecordUpdateVO> result = robotRoomRecordRpc.getRobotRoomRecordById(roomId);
        RobotRoomRecordUpdateVO updateVO = result.getData();
        if(updateVO != null){
            List<String> list = updateVO.getEmpList().stream().map(RobotRoomRecordEmpRelVO::getEmpId).collect(Collectors.toList());
            String empNames = userRpc.getEmployeeName(StringUtils.join(list, ","));
            updateVO.setEmpNames(empNames);
        }
        return result;
    }


    /**
     * 处理 智能机器人班机柜间巡检记录 中的异常，在巡检记录中只是改变状态
     * @param recordId
     * @return
     */
    @PostMapping("/deal")
    @ApiOperation("处理 智能机器人班机柜间巡检记录 中的异常")
    public CommonResult<Integer> dealRobotRoomRecordException(@RequestParam(required = true) String recordId){
        return robotRoomRecordRpc.dealRobotRoomRecordException(recordId);
    }


    /**
     * 获取 后台管理中巡检记录列表
     * @return
     */
    @GetMapping("/admin/recordList")
    @ApiOperation("获取 后台管理中巡检记录列表")
    public CommonResult<List<AdminRecordListVO>> getAdminRecordList(){
        CommonResult<List<AdminRecordListVO>> result = robotRoomRecordRpc.getAdminRecordList();
        List<AdminRecordListVO> list = result.getData();
        list.forEach(adminRecordListVO -> {
            if(!StringUtils.isEmpty(adminRecordListVO.getEmpIds())){
                adminRecordListVO.setEmpRels(userRpc.getEmployeeName(adminRecordListVO.getEmpIds()));
            }
        });
        return result;
    }

    /**
     * 后台--根据查询条件 分页获取所有的智能机器人班机柜间巡检记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllAdminByQuery")
    @ApiOperation("后台--根据查询条件分页获取所有的智能机器人班机柜间巡检记录")
    public CommonResult<PageResult<RobotRoomRecordAdminPageVO>> getAdminPageRobotRoomRecord(AdminRecordPageDTO pageDTO){
        String level2DeptId = pageDTO.getLevel2DeptId();
        String deptId = pageDTO.getDeptId();
        if(!StringUtils.isEmpty(level2DeptId)){
            pageDTO.setQueryDeptId(deptRpc.getDeptIdList(level2DeptId).getData());
        }else if(!StringUtils.isEmpty(deptId)){
            pageDTO.setQueryDeptId(deptRpc.getDeptIdList(deptId).getData());
        }
        CommonResult<PageResult<RobotRoomRecordAdminPageVO>> result = robotRoomRecordRpc.getAdminPageRobotRoomRecord(pageDTO);
        List<RobotRoomRecordAdminPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getEmpIds()))
                pageVO.setEmpNames(userRpc.getEmployeeName(pageVO.getEmpIds()));
        });
        return result;
    }

}
